package com.dongzili.demo.customer.fulltext.analysis;

import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * ik分词
 */
@Slf4j
public class IKAnalyzerSupport {

    private static final Logger logger = LoggerFactory.getLogger(IKAnalyzerSupport.class);

    /**
     * IK分词
     * @param target
     * @return
     */
    public static List<String> iKSegmenterToList(String target) {
        if (StringUtils.isEmpty(target)){
            return new ArrayList<>();
        }
        List<String> result = new ArrayList<>();
        StringReader sr = new StringReader(target);
        // 关闭智能分词 (对分词的精度影响较大)
        IKSegmenter ik = new IKSegmenter(sr, false);
        Lexeme lex;
        try {
            while((lex=ik.next())!=null) {
                String lexemeText = lex.getLexemeText();
                result.add(lexemeText);
            }
        }catch (IOException ignore){
            return new ArrayList<>();
        }
        //LOGGER.info("company:{}, iKSegmenterToList:{}", target, JSON.toJSON(result));
//        System.out.println("ik: " + String.join(",",result));
        return result;
    }


    public static void main(String[] args) throws Exception {
        String text = "先放后导先厚再薄课标教科书语文五年级下彩色的非洲教学片断与评析";
        String text2 = "先放后导";
        String text3 = "“归一、归总应用题”导学设计"; //["归","一","归总","应用题","导","学","设计"]
        logger.info(JSONArray.toJSONString(iKSegmenterToList(text3)));

//        List<SegToken> tokens = segment.process(text, JiebaSegmenter.SegMode.INDEX);
//        logger.info(JSONArray.toJSONString(tokens));
    }

}
